import {
  GETDATA,
  E
} from '../utils/R'
import {
  cloneDeep
} from 'lodash'
export default {
  // 命名空间 (必填)
  namespace: 'centerPage',
  // 数据
  state: {
    inSchool: 0,
    todayVisit: 0
  },
  // 路由监听
  subscriptions: {
    setup({
      dispatch,
      history
    }) {
      return history.listen((location, action) => {
        // 参数可以直接简写成{pathname}
        if (location.pathname === '/one') {
          dispatch({
            type: 'getCenterPageData'
          });
        }
      });
    },
  },
  // 异步请求
  effects: {
    * getCenterPageData({
      payload
    }, {
      call,
      put
    }) {
      const {
        data
      } = yield call(GETDATA, E);
      if (data) {
        yield put({
          type: 'setData',
          payload: data[0],
        });
      }
    },
  },
  // 同步操作
  reducers: {
    setData(state, action) {
      let newArr = cloneDeep(state)
      newArr.inSchool = action.payload
      console.log(newArr)
      return newArr
    },
  },
};